package yxy.game.pm2.module.chat;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.redisson.api.RMap;
import org.redisson.api.RSet;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.game.pm2.bean.Constant;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.module.BaseGameRedisBean;

import java.util.HashSet;
import java.util.Set;

/**
 * 全局黑名单
 *
 * <pre>
 *     定时清除数据程序请看{@link yxy.cherry.gm.task.Chat.CleanChatBlackTask}
 * </pre>
 *
 * @ClassName AllChatBlackModule
 * @Author jingtao-zheng
 * @Date 2022/8/15 17:15
 * @Version 1.0
 **/
public class AllChatBlackModule extends BaseGameRedisBean {
    /**
     * 全局黑名单添加
     */
    static public boolean addAllChatBlack(String playerCode, String playerBlackCode) {
        //检查白名单
        RSet<String> allChatWhite = AllChatWhiteModule.getAllChatWhite();
        if (allChatWhite != null && allChatWhite.size() != 0) {
            boolean contains = allChatWhite.contains(playerBlackCode);
            //如果是在白名单中直接返回
            if (contains) {
                return true;
            }
        }
        RMap<String, Report> map = null;
        try {
            map = RedisManager.client(redis()).getMap(key());
        } catch (RedisException e) {
            e.printStackTrace();
        }
        Report report = map.get(playerBlackCode);
        //第一次被举报 初始化
        if (report == null) {
            report = new Report();
            report.setUpdateTime(DateTime.now());
            Set<String> reportSet = new HashSet<>();
            reportSet.add(playerCode);
            report.setPlayerCodes(reportSet);
            map.put(playerBlackCode, report);
            return true;
        }
        //多次被举报
        //1.1检查更新时间
        DateTime updateTime = report.getUpdateTime();
        DateTime nowTime = DateTime.now();
        String updateTimeStr = updateTime.toString("yyyy-MM-dd");
        String nowTimeStr = nowTime.toString("yyyy-MM-dd");
        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd");
        DateTime parse1 = DateTime.parse(updateTimeStr, format);
        DateTime parse2 = DateTime.parse(nowTimeStr, format);
        //更新时间不一致
        if (parse1.getMillis() != parse2.getMillis()) {
            report.setUpdateTime(nowTime);
            //1.2 清除举报人列表,并新加举报人
            HashSet<String> playerSet = new HashSet<>();
            playerSet.add(playerCode);
            report.setPlayerCodes(playerSet);
            map.put(playerBlackCode, report);
            return true;
        } else {
            //更新时间一致
            //检测是否是今天重复举报
            Set<String> playerCodes = report.getPlayerCodes();
            boolean contains = playerCodes.contains(playerCode);
            if (contains) {
                //重复举报
                return false;
            }
            playerCodes.add(playerCode);
            if (playerCodes.size() >= Constant.REPORT_NUMS) {
                //获取被举报人信息
                PlayerBean playerBean = PlayerBean.get(playerBlackCode);
                int viplevel = playerBean.getViplevel();
                //vip等级是否大于2
                if (viplevel <= 2) {
                    //加入热点数据
                    HotSpotDataModule.addHotSpotData(playerBlackCode);
                    //加入禁言名单禁言10天
                    DateTime dateTime = nowTime.plusDays(Constant.BANNED_DAYS);
                    Banned.getInstance().set(playerBean, dateTime);
                }
            }
            map.put(playerBlackCode, report);
            return true;
        }
    }

    /**
     * 全局黑名单获取
     */
    static public Set<String> getAllChatBlack() {
        RMap<String, Report> map = null;
        try {
            map = RedisManager.client(redis()).getMap(key());
        } catch (RedisException e) {
            e.printStackTrace();
        }
        if (map != null) {
            return map.keySet();
        }
        return null;
    }

    //===================================redis
    static public String key() {
        return String.format("CHAT:ALLBLACK");
    }

    @Override
    public String lockKey() {
        return String.format("lock:%s", key());
    }
}
